import { clear_gun, get_gun } from 'base/api';
import { s_psn, g_db } from 'db/state';
import { uPrint, uJob, uReal, cline, obj_str } from 'sui';
import { put_log } from 'base/ctl/log';
import { msn_info, is_new, has_pos } from 'use/info';
import { insert_box, calc_pos } from './cmd';

const ck_pos = (psn, ck_psn=true) => {
    if (ck_psn) {
        if (psn.length > 10) {
            return ["条码位数错误"];
        }
    }

    const v_tray = g_db.boxtray();
    const v_level = g_db.boxlv();

    if (!v_tray || !v_level) {
        return ["请先设置托盘规格和层数", "no"];
    }

    if (!is_new(psn)) {
        return ["条码已经入箱", "no"];
    }

    const sum = g_db.sum() + 1;

    if (sum > v_tray * v_level) {
        return ["已经达到最大装箱容量", "no"];
    }

    const pos = calc_pos(sum);

    if (has_pos(pos)) {
        return ["此箱中间有空位，不允许放入新圈，请换箱子", "no"];
    }

    return [null, pos, sum];
};

const do_code = (stepnum, step, umd, watch) => {
    const psn = get_gun(step);
    if (!psn) {
        watch.startWatch();
        return;
    }

    put_log("编码", psn, "扫描轴承编码");

    clear_gun();
    s_psn(psn);

    const input = g_db.is_input();
    g_db.input(false);

    umd.set("psn", psn);

    const herr = (e) => {
        umd.setErr(e);
        watch.startWatch();
    };

    const [e, pos, sum] = ck_pos(psn);
    if (e) {
        return herr(e);
    }

    const hInsert = () => {
        insert_box(g_db.box(), psn, pos, () => {
            const cmd = {
                line: cline(),
                tp: g_db.boxtp(),
                box: g_db.box(),
                sn: psn,
            };

            uJob(null, "pick/add", cmd);

            msn_info(psn, pos);

            let num = g_db.num() + 1;

            g_db.s_num(num);
            g_db.s_sum(sum);

            umd.mset({
                sum: sum,
                num: num,
                err: "",
            });

            watch.startWatch();
        }, e => herr("入箱出错, 请重新扫码"));
    };

    if (input) {
        uReal(null, "pair/find",
            {
                line: cline(),
                tp: g_db.boxtp(),
                sn: psn,
            },
            r => herr(`此物料已入箱，箱号: ${obj_str(r, "box")}`),
            e => hInsert(),
        );
    } else {
        hInsert();
    }
};

const do_tagqr = (stepnum, step, umd, watch) => {
    const psn = get_gun(step);
    if (!psn) {
        watch.startWatch();
        return;
    }

    put_log("编码", psn, "扫描轴承编码");

    clear_gun();
    s_psn(psn);

    umd.set("psn", psn);

    const herr = (e) => {
        umd.setErr(e);
        watch.startWatch();
    };

    if (psn.length > 10) {
        return herr("条码位数错误");
    }

    const lub = g_db.lub();
    if (!lub) {
        return herr("请设置润滑类型");
    }

    const seal_sn = g_db.seal();

    if (!seal_sn) {
        return herr("请设置密封盖类型");
    }

    const cmd = {
        line: cline(),
        sn: psn,
        lub: lub,
        seal: seal_sn,
    };

    uJob(null, "goods/mark", cmd,
        r => {
            const tech_tag = obj_str(r, "tag");

            const cmd = {
                ...r,
                title: tech_tag,
            };

            uPrint(null, cmd);

            cmd["err"] = "";

            umd.mset(cmd);

            watch.startWatch();
        },
        e => herr("生成标签错误"));
};

export {do_tagqr, ck_pos, do_code};
